/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.dao.db4o;

import com.db4o.config.annotations.Indexed;

/**
 * Base entity for enumerations that typically has unique code and label and optional
 * description.
 */
public abstract class BaseEnum extends BaseEntity {

    public static final int CODE_MAXLEN = 16;
    public static final int LABEL_MAXLEN = 32;
    public static final int DESCRIPTION_MAXLEN = 256;

    private static final Column _CODE = new Column("code");
    private static final Column _LABEL = new Column("label");
    private static final Column _DESCRIPTION = new Column("description");

    /**
     * Internal unique enumeration item code because {@link #code}
     * is shared over subclasses.
     */
    @Indexed
    @Unique
    private String uniqueCode;

    /**
     * Internal unique enumeration item label because {@link #label}
     * is shared over subclasses.
     */
    @Indexed
    @Unique
    private String uniqueLabel;

    @Indexed
    private String code;

    @Indexed
    private String label;

    private String description = null;

    //----------------------------------------------------------------------

    public BaseEnum(String code, String label) {
        checkNotNullNotEmpty(_CODE, code);
        checkMaxLen(_CODE, code, CODE_MAXLEN);
        checkNotNullNotEmpty(_LABEL, label);
        checkMaxLen(_LABEL, label, LABEL_MAXLEN);

        this.uniqueCode = getClass().getSimpleName()+"["+code;
        this.uniqueLabel = getClass().getSimpleName()+"["+label;
        this.code = code;
        this.label = label;
    }

    public String getCode() {
        return code;
    }

    public String getLabel() {
        return label;
    }

    public String getDescription() {
        return description;
    }

    public BaseEnum setDescription(String description) {
        checkMaxLen(_DESCRIPTION, description, DESCRIPTION_MAXLEN);
        this.description = description;
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append("{code='").append(code).append('\'');
        sb.append(", label='").append(label).append('\'');
        sb.append(", description='").append(description).append('\'');
        sb.append('}');
        return sb.toString();
    }

}
